/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufpr.inf.escel.manager.util;

import br.ufpr.inf.escel.consts.Constantes;
import java.io.BufferedReader;
import java.io.FileReader;

/**
 * Classe que le os parametros passados pelo
 * arquivo de configuração
 * @author gustavo
 */
public class Init {

    /** Reads the parameters in the given file. Skips over empty lines and lines starting with a <code>#</code>.
     *
     * @param filename the name of the file which contains the parameters
     */
    public static String getParam(String param) {
        boolean readCorrectly = true;
        try {
            FileReader fr = new FileReader("pisa/" + Constantes.PISA_CONFIG);
            BufferedReader br = new BufferedReader(fr);
            String paramName = "", paramValue = "";
            String nextParam = br.readLine();
            while (nextParam != null) {

                /* continue with next line, if no key found */
                if (nextParam.length() == 0) {
                    nextParam = br.readLine();
                    continue;
                }

                /* forget about comments */
                if (nextParam.charAt(0) == '#') {
                    nextParam = br.readLine();
                    continue;
                }

                /* read next line, split it and extract name and value */
                String[] str = nextParam.split(" ");
                paramName = str[0];
                paramValue = str[1];

                if (paramName.equals(param)) {
                    return paramValue;
                }
                nextParam = br.readLine();
            }
            br.close();
            fr.close();
        } catch (Exception ex) {
            System.err.println("Problem reading the parameter file! Terminating!");
            ex.printStackTrace();
        }
        if (!readCorrectly) {
            System.out.println("Could not read parameter file.");
            System.exit(1);
        }
        return null;
    }
}
